package com.net.xpay.operator.controller.operator.common;

import com.google.common.base.Strings;
import com.net.xpay.operator.secutiry.CurrentOperator;
import com.net.xpay.operator.secutiry.Operator;
import com.net.common.exception.BusinessException;
import com.net.common.model.ResultData;
import com.net.common.util.NetUtil;
import com.net.xpay.common.domain.CheckCode;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.domain.operator.OperatorUser;
import com.net.xpay.common.enums.user.CheckCodeType;
import com.net.xpay.common.manager.UserManager;
import com.net.xpay.common.manager.operator.OperatorUserManager;
import com.net.xpay.common.service.CheckCodeService;
import com.net.xpay.core.constant.PoseidonErrorCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

/**
 * @author  on 08/02/2018.
 */
@RestController
@RequestMapping("/common/check-code")
@Api(tags = "公共/验证码", description = "公共 验证码 net")
@Slf4j
public class CommonCheckCodeController {

    @Autowired
    private CheckCodeService checkCodeService;
    @Autowired
    private OperatorUserManager operatorUserManager;
    @Autowired
    private UserManager userManager;



    @ApiOperation("获取登录验证码")
    @GetMapping("/login")
    @ApiImplicitParam(name = "mobile", value = "手机号")
    public ResultData<Boolean> getLoginCheckCode(HttpServletRequest request, String mobile) {
        if (Strings.isNullOrEmpty(mobile)) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "手机号不能为空");
        }
        OperatorUser operatorUser = operatorUserManager.findByMobile(mobile);
        if (operatorUser == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "该手机号尚未注册");
        }
        return new ResultData<>(sendCheckCodeForMobile(request, mobile, CheckCodeType.OPERATOR_USER_LOGIN));
    }

    @ApiOperation("获取注册验证码")
    @GetMapping("/register")
    @ApiImplicitParam(name = "mobile", value = "手机号")
    public ResultData<Boolean> getRegisterCheckCode(HttpServletRequest request, String mobile) {
        if (Strings.isNullOrEmpty(mobile)) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "手机号不能为空");
        }
        return new ResultData<>(sendCheckCodeForMobile(request, mobile, CheckCodeType.OPERATOR_USER_REGISTER));
    }

    @ApiOperation("获取修改密码验证码")
    @GetMapping("/modify-mobile")
    @ApiImplicitParam(name = "mobile", value = "手机号")
    public ResultData<Boolean> getModifyMobileCheckCode(@ApiIgnore @CurrentOperator Operator operator, HttpServletRequest request) {
        return new ResultData<>(sendCheckCodeForCurrentUser(operator,request, CheckCodeType.OPERATOR_USER_MODIFY_PASSWORD));
    }

    @ApiOperation("获取添加关联公司验证码")
    @GetMapping("/add-binding-user")
    @ApiImplicitParam(name = "mobile", value = "手机号")
    public ResultData<Boolean> getAddBinDingUserCheckCode(@ApiIgnore @CurrentOperator Operator operator, String mobile, HttpServletRequest request) {
        User user = userManager.findByMobile(mobile);
        if (user == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "公司商户账号未注册");
        }
        return new ResultData<>(sendCheckCodeForMobile(request, mobile, CheckCodeType.OPERATOR_ADD_BINDING_USER));
    }

    private boolean sendCheckCodeForMobile(HttpServletRequest request, String mobile, CheckCodeType checkCodeType) {
        CheckCode checkCode = checkCodeService.sendCheckCode(mobile, checkCodeType, NetUtil.getRequestRealIp(request));
        return checkCode != null && !Strings.isNullOrEmpty(checkCode.getCode());
    }

    private boolean sendCheckCodeForCurrentUser(Operator operator, HttpServletRequest request, CheckCodeType checkCodeType) {
        if (operator == null) {
            throw new BusinessException(PoseidonErrorCode.NEED_LOGIN, "用户未登陆");
        }
        OperatorUser operatorUser = operatorUserManager.getById(operator.getOperatorId());
        return sendCheckCodeForMobile(request, operatorUser.getMobile(), checkCodeType);
    }
}
